package com.durian.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.durian.enums.IsDeleted;
import com.durian.utils.UUIDUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * MyBatisPlus自动填充
 *
 * @author Durian
 * @date 2023/11/28
 */
@Slf4j
@Component
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 订单生成日期
        this.strictInsertFill(metaObject, "orderCreateTime", LocalDateTime.class, LocalDateTime.now());
        // 生成订单号
        this.strictInsertFill(metaObject, "orderNumber", String.class, UUIDUtils.OrderNumber());
        // 即入购物车的时间
        this.strictInsertFill(metaObject, "bookAddDate", LocalDateTime::now, LocalDateTime.class);
        // 插入逻辑删除字段
        this.strictInsertFill(metaObject, "deleted", Integer.class, IsDeleted.NO.getValue());
        // 生成书籍号
        this.strictInsertFill(metaObject, "bookNumber", String.class, UUIDUtils.BookNumber());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ...."); 
    }
    
}
